﻿Imports MeoTom.Core

Partial Public Class Category
    Inherits System.Web.UI.Page

    Private _CategoryName As String

    Public Property CategoryName() As String
        Get
            Return _CategoryName
        End Get
        Set(ByVal value As String)
            _CategoryName = value
        End Set
    End Property

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsNumeric(Request.QueryString("CatID")) Or Request.QueryString("CatID") = vbNullString Or Not IsNumeric(Request.QueryString("SubID")) Or Request.QueryString("SubID") = vbNullString Then
            Response.Redirect("/Error?e=404")
        Else
            WriteCat(Integer.Parse(Request.QueryString("CatID")), Integer.Parse(Request.QueryString("SubID")))
        End If

        Page.Title = CategoryName & " - " & Configs.WebTitle
        Dim metaTag As New HtmlMeta()
        metaTag.Name = "description"
        metaTag.Content = CategoryName & " - " & Configs.WebTitle
    End Sub

    Public Sub WriteCat(ByVal _CatID As Integer, ByVal _SubCat As Integer)
        If _SubCat = 0 Then
            _SubCat = _CatID
        End If

        Dim strOut As New StringBuilder
        strOut.Append("<ul>")
        strOut.Append("     <li class=""first""><a href=""/"">Trang chủ</a></li>")
        Using dc As New MeoTomDataContext
            Dim categorySource = From c In dc.ArticleCategories Where c.CatID = _CatID Or c.CatID = _SubCat And c.SiteID = Configs.SiteID Order By c.SubCat Ascending Select c.CatID, c.Title, c.TitleNone, c.SubCat
            For Each CatItem In categorySource
                If _CatID = CatItem.CatID Then
                    strOut.Append("<li>").Append(Server.HtmlEncode(CatItem.Title)).Append("</li>")
                    CategoryName = Server.HtmlEncode(CatItem.Title)
                Else
                    strOut.Append("<li><a href=""/c").Append(CatItem.CatID).Append("s").Append(CatItem.SubCat).Append("/").Append(CatItem.TitleNone).Append(""">").Append(Server.HtmlEncode(CatItem.Title)).Append("</a></li>")
                End If
            Next
        End Using
        strOut.Append("</ul>")
        nav2.InnerHtml = strOut.ToString
        nav2.InnerHtml &= "<script language=""javascript"" type=""text/javascript"">MenuV2.selectedById(" & _CatID & "," & _SubCat & ");</script>"
    End Sub

    Public Function GenHtml(ByVal articlesID As String, ByVal Title As String, _
                            ByVal abstract As String, ByVal totalComment As String, _
                            ByVal viewCount As String, ByVal articlePath As String, _
                            ByVal isMedia As String, ByVal isPicture As String, _
                            ByVal releaseDate As String, ByVal userName As String, _
                            ByVal postVisible As String) As String
        Dim strMedia As String = ""
        Const strVideo As String = "&nbsp;<img src=""/images/hasVideo.gif"" style=""padding:0 0px 5px 0;float:none;"" width=""15"" height=""9"" alt=""Video"" />"
        Const strImage As String = "&nbsp;<img src=""/images/HasImage.gif"" style=""padding:0 0px 5px 0;float:none;"" width=""10"" height=""10"" alt=""Images"" />"

        If isMedia = "True" Then
            strMedia &= strVideo
        End If

        If IsPicture = "True" Then
            strMedia &= strImage
        End If

        Dim strOut As New StringBuilder
        If postVisible = "2" Then
            strOut.Append(" <div class=""post-detail fl"">")
            strOut.Append("     <div class=""post-detail-left fl"">")
            strOut.Append("         <div class=""post-detail-bg""><span class=""total-cm"">").Append(totalComment).Append("</span><br />Comments<br />").Append(viewCount).Append(" Views</div>")
            strOut.Append("     </div>")
            strOut.Append("     <div class=""post-detail-right fl"">")
            strOut.Append("         <h3><a style=""color:#FF0000;"" href=""").Append(articlePath).Append(""" title=""").Append(Title).Append(""">").Append(Title).Append("</a>").Append(strMedia).Append("</h3>")
            strOut.Append("         <div class=""post-desc"">").Append(Server.HtmlDecode(abstract).ToString).Append("...").Append("</div>")
            strOut.Append("         <div class=""post-info""><a href=""").Append(articlePath).Append(""" title=""").Append(Title).Append(""">").Append(Format(DateTime.Parse(releaseDate).AddHours(Configs.TimeLine), "dd-MM-yyyy HH:mm tt")).Append("</a> bởi <a href=""/Users/").Append(userName).Append(""" title=""").Append(userName).Append(""">").Append(userName).Append("</a> danh mục <span id=""ArticleCat___").Append(articlesID).Append("""></span></div>")
            strOut.Append("     </div>")
            strOut.Append(" </div>")
            strOut.Append(" <div class=""hr cut94 fl"">&nbsp;</div>")
        Else
            strOut.Append(" <div class=""post-detail fl"">")
            strOut.Append("     <div class=""post-detail-left fl"">")
            strOut.Append("         <div class=""post-detail-bg""><span class=""total-cm"">").Append(totalComment).Append("</span><br />Comments<br />").Append(viewCount).Append(" Views</div>")
            strOut.Append("     </div>")
            strOut.Append("     <div class=""post-detail-right fl"">")
            strOut.Append("         <h3><a href=""").Append(articlePath).Append(""" title=""").Append(Title).Append(""">").Append(Title).Append("</a>").Append(strMedia).Append("</h3>")
            strOut.Append("         <div class=""post-desc"">").Append(Server.HtmlDecode(abstract).ToString).Append("...").Append("</div>")
            strOut.Append("         <div class=""post-info""><a href=""").Append(articlePath).Append(""" title=""").Append(Title).Append(""">").Append(Format(DateTime.Parse(releaseDate).AddHours(Configs.TimeLine), "dd-MM-yyyy HH:mm tt")).Append("</a> bởi <a href=""/Users/").Append(userName).Append(""" title=""").Append(userName).Append(""">").Append(userName).Append("</a> danh mục <span id=""ArticleCat___").Append(articlesID).Append("""></span></div>")
            strOut.Append("     </div>")
            strOut.Append(" </div>")
            strOut.Append(" <div class=""hr cut94 fl"">&nbsp;</div>")
        End If

        Return strOut.ToString
    End Function

    Private Sub lqDataSource_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles lqDataSource.Selecting
        If Not IsNumeric(Request.QueryString("CatID")) Or Request.QueryString("CatID") = vbNullString Then
            Response.Redirect("/Error?e=404")
        Else
            Dim _CatID As Integer = Integer.Parse(Request.QueryString("CatID"))

            Using dc As New MeoTomDataContext
                e.Result = dc.Justin_Article_GetByCat(_CatID, Configs.SiteID).ToList
            End Using
        End If
    End Sub

    Private Sub lstArticle_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstArticle.PreRender
        If lstArticle.Items.Count = 0 Then
            Response.Redirect("/Error?e=NoArticle", False)
        End If
    End Sub

    Private Sub dtPgArticle_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtPgArticle.PreRender
        For Each control As UI.Control In dtPgArticle.Controls
            For Each c As UI.Control In control.Controls
                If TypeOf c Is HyperLink Then
                    Dim currentLink As HyperLink = DirectCast(c, HyperLink)
                    Dim match As Match = Regex.Match(currentLink.NavigateUrl, "Page=(?<Page123>.*?).*")
                    If match.Success Then
                        If Request.QueryString("Page") = vbNullString Then
                            currentLink.NavigateUrl = Request.RawUrl & "?" & match.Value
                        Else
                            currentLink.NavigateUrl = Request.RawUrl.Replace("Page=" & Request.QueryString("Page"), "") & match.Value
                        End If
                    End If
                End If
            Next
        Next
    End Sub
End Class